package cn.texous.demo.dnw.stream;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.ToString;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * insert description here.
 *
 * @author Showa.L
 * @since 2019/10/12 11:29
 */
public class TestStream {

    public static void main(String[] args) {


        List<String> list = new ArrayList<>();
        String strs = "1,2,3,4,5,6,7,8,9";

//        Optional.ofNullable(strs)
//                .map(ss -> strs.split(","))
//                .arrayStream(String.class)
//                .forEach(list::add);
//
//        list.stream().forEach(System.out::println);
//
//        List<Us> usList = new ArrayList<>();
//        usList.add(new Us("1"));
//        usList.add(new Us("2"));
//        usList.add(new Us("3"));
//        usList.add(new Us("4"));
//        usList.add(new Us("5"));
//
//        Optional.of(usList)
//                .map(ls -> ls.toArray())
//                .arrayStream(Us.class).forEach(System.out::println);

        Optional.ofNullable(strs)
                .map(s -> strs.split(","))
                .map(TestStream::stream)
                .ifPresent(st -> st.forEach(list::add));

        Optional.ofNullable(strs)
                .map(s -> strs.split(","))
                .ifPresent(ss -> {
                    Stream.of(ss).forEach(list::add);
                });


        String str = null;
        Stream.of(str)
                .findFirst()
                .ifPresent(System.out::println);

        list.stream().forEach(System.out::println);

    }

    public static <T> Stream<T> stream(T[] ts) {
        if (ts == null)
            return Stream.empty();
        return Stream.of(ts);
    }

    static <T> Stream<T> streamopt(Optional<T> opt) {
        if (opt.isPresent())
            return Stream.of(opt.get());
        else
            return Stream.empty();
    }

    @Data
    @ToString
    @AllArgsConstructor
    private static class Us {
        private String id;

    }

}
